package cn.globalph.housekeeper.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import cn.globalph.housekeeper.R;
import cn.globalph.housekeeper.data.model.UpdateModel;


/**
 * Created by yuan on 2017/9/11 0011.
 */

public class UpdateAppDialog extends DialogFragment implements
        DialogInterface.OnKeyListener, View.OnClickListener {
    public static final String TAG = "UPDATE_APP";
    TextView mUpdateContent;
    private View view;
    FrameLayout closeLayout;
    TextView mSkipThisVersion;
    private VisitorListener listener;
    private boolean forceUpdate;
    private Button ensureBtn;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        view = View.inflate(getContext(), R.layout.update_dialog, null);
        mUpdateContent = view.findViewById(R.id.update_content);
        closeLayout = view.findViewById(R.id.close_dialog);
        mSkipThisVersion = view.findViewById(R.id.skip_this_version);
        ensureBtn = view.findViewById(R.id.ensure_button);

        mUpdateContent.setOnClickListener(this);
        closeLayout.setOnClickListener(this);
        mSkipThisVersion.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
        AlertDialog dialog = new AlertDialog.Builder(getContext())
                .setView(view)
                .create();

        dialog.setOnKeyListener(this);

        UpdateModel updateModel = getArguments().getParcelable("update");
        if (updateModel != null) {
            if (updateModel.isForceUpdate()) {
                this.forceUpdate = true;
                closeLayout.setVisibility(View.GONE);
                mSkipThisVersion.setVisibility(View.GONE);
            }
            if(!updateModel.isSkipAllowed()){
                mSkipThisVersion.setVisibility(View.GONE);
            }
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            String remark = updateModel.getRemarks();
            if (!TextUtils.isEmpty(remark))
                remark = remark.replaceAll("\\n", "<br/>");
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                mUpdateContent.setText(Html.fromHtml(remark, Html.FROM_HTML_MODE_LEGACY));
            } else {
                mUpdateContent.setText(Html.fromHtml(remark));
            }
        }
        return dialog;
    }

    public UpdateAppDialog setListener(VisitorListener listener) {
        this.listener = listener;
        return this;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setLayout(dp2px(300), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }



    public int dp2px(float dpVal) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics()));
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (manager.isDestroyed()) {
                return;
            }
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!forceUpdate)
                dismiss();
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ensure_button:
                if (listener != null)
                    listener.onClickUpload();
                dismiss();
                break;
            case R.id.close_dialog:
                if (listener != null)
                    listener.onClickClose();
                dismiss();
                break;
            case R.id.skip_this_version:
                if (listener != null)
                    listener.onSkip();
                dismiss();
                break;
        }
    }

    public interface VisitorListener {
        void onClickUpload();

        void onClickClose();

        void onSkip();
    }
}
